#include "mainwindow.h"
#include "menuview.h"
#include "paletteview.h"
#include "toolkit.h"
#include <QGraphicsPixmapItem>
#include <QCoreApplication>
#include <QFile>
#include "brushitem.h"
#include <QSettings>
#include "touchpixmapitem.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    m_menuview = new menuview(this);
    m_menuview->setGeometry(0,0,640,360);
    connect(m_menuview,SIGNAL(showpalette(PALOPENTYPE, int)),this,SLOT(showpalette(PALOPENTYPE, int)));
    m_menuview->showFullScreen();
}

MainWindow::~MainWindow()
{

}

void MainWindow::showpalette(PALOPENTYPE palopentype, int icurpaper)
{
    m_menuview->hide();
    delete m_menuview;
    paletteview* pview = new  paletteview(this);
    pview->setGeometry(0.0,0.0,640.0,360.0);
    pview->m_curselPaper = icurpaper;

    switch(palopentype)
    {
    case NEW_PALOPENTP:
        {
           pview->m_bgitem = pview->m_Scene->addPixmap(QPixmap(":/image/paper/paper(1).jpg","jpg"));
        }
        break;
    case PHOTO_PALOPENTP:
        {
            QImage image;
            image.load(ToolKit::m_strImagePath);
            QPixmap pixmapToShow = QPixmap::fromImage( image.scaled(QSize(465,360), Qt::KeepAspectRatioByExpanding) );
            pview->m_bgitem = pview->m_Scene->addPixmap(pixmapToShow);
        }
        break;
    case GOON_PALOPENTP:
        {
            QString str;
            if(icurpaper != -1)
            {
                str = QString(":/image/paper/paper(%1).jpg").arg(icurpaper + 1);
                pview->m_bgitem = pview->m_Scene->addPixmap(QPixmap(str,"jpg"));
            }
            else
            {
                str = QCoreApplication::applicationDirPath() + "/selfbg.png";
                if(QFile::exists(str))
                {
                    pview->m_bgitem = pview->m_Scene->addPixmap(QPixmap(str,"png"));
                }
                else
                {
                    pview->m_bgitem = pview->m_Scene->addPixmap(QPixmap(":/image/paper/paper(1).jpg","jpg"));
                }
            }

            str = QCoreApplication::applicationDirPath() + "/selfpicture.png";
            if(QFile::exists(str))
            {
                pview->m_painter->drawPixmap(0,0,QPixmap(str));
                pview->m_paletteItem->update();
            }

            QSettings settings("MySoft", "Star Runner");

            int icount = settings.value("sticker/number",0).toInt();

            for(int i = 0; i < icount; ++i)
            {
                QString str = QString("sticker/%1").arg(i),strtemp;

                strtemp = str + "type";
                int itype = settings.value(strtemp,1).toInt();
                QString strimage = QString(":/image/sticker/sticker (%1).png").arg(itype);
                TouchPixmapItem* touchitem = new TouchPixmapItem();
                touchitem->setPixmap(QPixmap(strimage,"png"));

                strtemp = str + "lock";
                touchitem->m_bLock = settings.value(strtemp,false).toBool();

                touchitem->m_type = itype;

                strtemp = str + "pos";
                touchitem->setPos(settings.value(strtemp,QPointF(200.0,200.0)).toPoint());

                strtemp = str + "z";
                touchitem->setZValue(settings.value(strtemp,1).toInt());

                strtemp = str + "scale";
                touchitem->setScale(settings.value(strtemp,1.0).toFloat());

                strtemp = str + "rotation";
                touchitem->setRotation(settings.value(strtemp,1.0).toFloat());

                pview->m_TouchPixmapItemList.append(touchitem);
                pview->m_Scene->addItem(touchitem);
            }
        }
        break;
    default:
        break;
    }
    pview->m_bgitem->setPos(70,0);
    pview->m_bgitem->setZValue(1.0);
    pview->showFullScreen();
}
